<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java线程池核心属性解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f9fafb;
            color: #374151;
        }
        .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(59, 130, 246, 0.2);
            z-index: -1;
            transition: all 0.2s ease;
        }
        .highlight:hover::after {
            height: 100%;
        }
        .code-block {
            font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
            background-color: #1e293b;
            color: #f8fafc;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .dropdown:hover .dropdown-content {
            display: block;
        }
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            width: 2px;
            height: 100%;
            background-color: #3b82f6;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl flex flex-col items-center text-center">
            <h1 class="serif text-4xl md:text-5xl font-bold mb-6">Java线程池核心属性解析</h1>
            <p class="text-xl md:text-2xl max-w-3xl leading-relaxed mb-8">深入理解线程池的关键参数配置与优化策略</p>
            <div class="flex flex-wrap justify-center gap-4">
                <a href="#core-concepts" class="bg-white text-blue-600 hover:bg-blue-50 px-6 py-3 rounded-full font-medium transition-all duration-300 transform hover:scale-105">
                    <i class="fas fa-brain mr-2"></i>核心概念
                </a>
                <a href="#visualization" class="bg-blue-800 hover:bg-blue-900 px-6 py-3 rounded-full font-medium transition-all duration-300 transform hover:scale-105">
                    <i class="fas fa-project-diagram mr-2"></i>可视化分析
                </a>
                <a href="#code-example" class="bg-white text-blue-600 hover:bg-blue-50 px-6 py-3 rounded-full font-medium transition-all duration-300 transform hover:scale-105">
                    <i class="fas fa-code mr-2"></i>代码示例
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8">
                <h2 class="serif text-2xl font-bold mb-4 text-gray-800">线程池的核心价值</h2>
                <p class="text-gray-700 mb-4 leading-relaxed">线程池是多线程编程中的核心组件，它通过复用线程资源、控制并发数量来提高系统性能和响应能力。<span class="highlight font-medium">Java的ThreadPoolExecutor</span>提供了丰富的配置选项，理解这些核心属性是优化线程池性能的关键。</p>
                <div class="flex items-center text-blue-600 mt-4">
                    <i class="fas fa-lightbulb mr-2"></i>
                    <span>专业提示：合理配置线程池参数可以显著提升系统吞吐量并减少资源消耗</span>
                </div>
            </div>
        </section>

        <!-- Core Concepts -->
        <section id="core-concepts" class="mb-16">
            <h2 class="serif text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-cogs mr-4 text-blue-500"></i>线程池核心属性详解
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- Card 1 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-blue-500 p-4 text-white">
                        <h3 class="serif text-xl font-bold flex items-center">
                            <i class="fas fa-microchip mr-2"></i>核心线程数 (corePoolSize)
                        </h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">线程池中始终保持的线程数量，即使这些线程处于空闲状态也会被保留在池中。</p>
                        <div class="bg-blue-50 p-4 rounded-lg">
                            <p class="text-sm text-gray-600"><i class="fas fa-info-circle mr-2 text-blue-500"></i>决定了线程池的基本线程容量，设置合理的核心线程数可以保证系统在低负载情况下也能够高效地处理任务。</p>
                        </div>
                    </div>
                </div>

                <!-- Card 2 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-purple-500 p-4 text-white">
                        <h3 class="serif text-xl font-bold flex items-center">
                            <i class="fas fa-expand mr-2"></i>最大线程数 (maximumPoolSize)
                        </h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">线程池中允许的最大线程数量，当工作队列满了且核心线程数已经达到时，会创建新线程。</p>
                        <div class="bg-purple-50 p-4 rounded-lg">
                            <p class="text-sm text-gray-600"><i class="fas fa-info-circle mr-2 text-purple-500"></i>合理设置可以处理高负载情况下的任务，防止系统过载或资源浪费。</p>
                        </div>
                    </div>
                </div>

                <!-- Card 3 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-green-500 p-4 text-white">
                        <h3 class="serif text-xl font-bold flex items-center">
                            <i class="fas fa-clock mr-2"></i>空闲线程存活时间 (keepAliveTime)
                        </h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">非核心线程在没有任务时保持活跃的时间，超过这个时间没有任务则会被回收。</p>
                        <div class="bg-green-50 p-4 rounded-lg">
                            <p class="text-sm text-gray-600"><i class="fas fa-info-circle mr-2 text-green-500"></i>在任务负载变化较大的场景中，适当的空闲时间可以帮助系统节省资源。</p>
                        </div>
                    </div>
                </div>

                <!-- Card 4 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-yellow-500 p-4 text-white">
                        <h3 class="serif text-xl font-bold flex items-center">
                            <i class="fas fa-list-ul mr-2"></i>工作队列 (workQueue)
                        </h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">存放等待执行的任务的队列，常见的队列有ArrayBlockingQueue、LinkedBlockingQueue等。</p>
                        <div class="bg-yellow-50 p-4 rounded-lg">
                            <p class="text-sm text-gray-600"><i class="fas fa-info-circle mr-2 text-yellow-500"></i>队列的类型和大小会影响线程池的性能和任务的处理方式。</p>
                        </div>
                    </div>
                </div>

                <!-- Card 5 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-red-500 p-4 text-white">
                        <h3 class="serif text-xl font-bold flex items-center">
                            <i class="fas fa-industry mr-2"></i>线程工厂 (threadFactory)
                        </h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">用于创建新线程的工厂，可以用来自定义线程的创建方式，如设置线程名称、线程优先级等。</p>
                        <div class="bg-red-50 p-4 rounded-lg">
                            <p class="text-sm text-gray-600"><i class="fas fa-info-circle mr-2 text-red-500"></i>通过自定义线程工厂，可以为线程池中的线程提供更好的管理和监控。</p>
                        </div>
                    </div>
                </div>

                <!-- Card 6 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-indigo-500 p-4 text-white">
                        <h3 class="serif text-xl font-bold flex items-center">
                            <i class="fas fa-ban mr-2"></i>拒绝策略 (handler)
                        </h3>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 mb-4">当线程池无法接受新任务时，如何处理这些任务的策略，如AbortPolicy、CallerRunsPolicy等。</p>
                        <div class="bg-indigo-50 p-4 rounded-lg">
                            <p class="text-sm text-gray-600"><i class="fas fa-info-circle mr-2 text-indigo-500"></i>根据系统的容错需求和任务处理的重要性选择合适的拒绝策略。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section id="visualization" class="mb-16">
            <h2 class="serif text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-project-diagram mr-4 text-blue-500"></i>线程池工作原理可视化
            </h2>
            
            <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
                <h3 class="serif text-xl font-bold mb-4 text-gray-800">线程池任务处理流程</h3>
                <div class="mermaid">
                    graph TD
                    A[提交新任务] --> B{核心线程是否空闲?}
                    B -->|是| C[使用核心线程执行任务]
                    B -->|否| D{工作队列是否已满?}
                    D -->|否| E[将任务加入工作队列]
                    D -->|是| F{是否达到最大线程数?}
                    F -->|否| G[创建新线程执行任务]
                    F -->|是| H[执行拒绝策略]
                    E --> I[等线程空闲后从队列获取任务]
                </div>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="serif text-xl font-bold mb-4 text-gray-800">线程池状态转换</h3>
                    <div class="mermaid">
                        stateDiagram-v2
                        [*] --> RUNNING
                        RUNNING --> SHUTDOWN: shutdown()
                        RUNNING --> STOP: shutdownNow()
                        SHUTDOWN --> TIDYING: 队列和线程池都为空
                        STOP --> TIDYING: 线程池为空
                        TIDYING --> TERMINATED: terminated()
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="serif text-xl font-bold mb-4 text-gray-800">拒绝策略对比</h3>
                    <div class="space-y-4">
                        <div class="p-4 bg-blue-50 rounded-lg">
                            <h4 class="font-bold text-blue-700 mb-2">AbortPolicy</h4>
                            <p class="text-sm text-gray-700">抛出RejectedExecutionException异常</p>
                        </div>
                        <div class="p-4 bg-green-50 rounded-lg">
                            <h4 class="font-bold text-green-700 mb-2">CallerRunsPolicy</h4>
                            <p class="text-sm text-gray-700">由调用者线程直接执行该任务</p>
                        </div>
                        <div class="p-4 bg-yellow-50 rounded-lg">
                            <h4 class="font-bold text-yellow-700 mb-2">DiscardPolicy</h4>
                            <p class="text-sm text-gray-700">直接丢弃任务，不做任何处理</p>
                        </div>
                        <div class="p-4 bg-red-50 rounded-lg">
                            <h4 class="font-bold text-red-700 mb-2">DiscardOldestPolicy</h4>
                            <p class="text-sm text-gray-700">丢弃队列中最老的任务，然后重新尝试提交</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Example -->
        <section id="code-example" class="mb-16">
            <h2 class="serif text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-code mr-4 text-blue-500"></i>线程池配置示例代码
            </h2>
            
            <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                <div class="bg-gray-900 px-4 py-3 flex items-center">
                    <div class="flex space-x-2 mr-4">
                        <span class="w-3 h-3 rounded-full bg-red-500"></span>
                        <span class="w-3 h-3 rounded-full bg-yellow-500"></span>
                        <span class="w-3 h-3 rounded-full bg-green-500"></span>
                    </div>
                    <span class="text-gray-300 text-sm">ThreadPoolDemo.java</span>
                </div>
                <div class="code-block p-6 overflow-x-auto">
                    <pre><code class="text-sm language-java">import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        // 创建线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            4,                     // corePoolSize - 核心线程数
            10,                    // maximumPoolSize - 最大线程数
            60,                    // keepAliveTime - 空闲线程存活时间
            TimeUnit.SECONDS,      // unit - 时间单位
            new LinkedBlockingQueue&lt;Runnable&gt;(),  // workQueue - 工作队列
            new ThreadPoolExecutor.CallerRunsPolicy()  // handler - 拒绝策略
        );

        // 提交任务到线程池
        for (int i = 0; i &lt; 20; i++) {
            final int taskId = i;
            executor.execute(() -> {
                System.out.println("Executing task " + taskId + " by " + 
                    Thread.currentThread().getName());
                try {
                    Thread.sleep(2000);  // 模拟任务处理
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
        }

        // 关闭线程池
        executor.shutdown();
    }
}</code></pre>
                </div>
            </div>

            <div class="mt-8 grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="serif text-xl font-bold mb-4 text-gray-800">参数配置建议</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>CPU密集型任务: corePoolSize = CPU核心数</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>IO密集型任务: corePoolSize = CPU核心数 * 2</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>keepAliveTime: 根据任务波动性设置，通常30-120秒</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>工作队列: IO密集型用SynchronousQueue，CPU密集型用有界队列</span>
                        </li>
                    </ul>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="serif text-xl font-bold mb-4 text-gray-800">监控与调优</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-chart-line text-blue-500 mt-1 mr-2"></i>
                            <span>监控线程池活动线程数、队列大小等指标</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-tachometer-alt text-purple-500 mt-1 mr-2"></i>
                            <span>根据实际负载动态调整核心线程数(Java 7+)</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-exclamation-triangle text-yellow-500 mt-1 mr-2"></i>
                            <span>避免无界队列导致内存溢出</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-user-shield text-red-500 mt-1 mr-2"></i>
                            <span>为关键任务设置适当的拒绝策略</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p>深入技术细节，探索编程之美</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors duration-300">
                        <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-sm">
                <p>© 2023 技术小馆 - 分享优质技术内容</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC, sans-serif',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            },
            state: {
                useMaxWidth: false,
                htmlLabels: true,
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>